package com.lishe.lishe.lishe.module.auth.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.ValidationUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcase on 2017/9/5.
 */

public class ActiveEndFragment extends AuthBaseFragment implements View.OnClickListener{

    public static final String TAG = ActiveEndFragment.class.getSimpleName();

    private ViewPager viewPager = null;

    private EditText inputText1,inputText2;

    public ActiveEndFragment() {
        super();
    }


    public static ActiveEndFragment newInstance(){
        ActiveEndFragment instance = new ActiveEndFragment();
        Bundle bundle = new Bundle();
        instance.setArguments(bundle);
        return instance;
    }


    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

        View containerFragment = inflater.inflate(R.layout.activity_auth_active_end, container, false);

        ImageButton back = (ImageButton) containerFragment.findViewById(R.id.back_title);
        back.setOnClickListener(this);

        inputText1 = (EditText) containerFragment.findViewById(R.id.inputText1);

        inputText2 = (EditText) containerFragment.findViewById(R.id.inputText2);

        Button submitBtn = (Button) containerFragment.findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);

        TextView redirectBtn1 = (TextView) containerFragment.findViewById(R.id.redirectBtn1);
        redirectBtn1.setClickable(true);
        redirectBtn1.setOnClickListener(this);


        return containerFragment;
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.back_title) {
//            if (viewPager != null) {
//                viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Active, true);
//            }

            if(mJumper!=null)
                mJumper.jumpToFragmentBack(FragmentAuthContants.TAB_INDEX_Active);


        } else if (view.getId() == R.id.redirectBtn1) {

            //用户协议
//            if (viewPager != null) {
//                //viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Reg, true);
//            }
//            if(mJumper!=null)
//                mJumper.jumpToFragmentForward(FragmentAuthContants.TAB_INDEX_RegEnd);


        } else if (view.getId() == R.id.submitBtn) {

            final String password = inputText1.getText().toString().trim();
            if(!ValidationUtils.isLimitPass(password)){
                Toast.makeText(getActivity(), R.string.LimitPass, Toast.LENGTH_SHORT).show();

                return;
            }


            HashMap params = new HashMap();
            params.put("mobile",AuthHttpHelper.phoneOfActive);
            params.put("password",password);
            params.put("checkCode",AuthHttpHelper.checkCodeOfActive);


            LoadingUtils.createLoadingDialog(getActivity(),"请稍等...",true);
            AuthHttpHelper.doActivite(getActivity(),params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {
                    //Log.e(TAG,dataString);
                    AuthHttpHelper.phoneOfActive = null;
                    AuthHttpHelper.checkCodeOfActive = null;
                    LoadingUtils.closeDialog();
                    inputText1.setText("");
                    inputText2.setText("");
                    //getActivity().finish();
                    if(mJumper!=null)
                        mJumper.jumpToFragmentBack(FragmentAuthContants.TAB_INDEX_Login);
                }

                @Override
                public void onFailure(String message) {
                    LoadingUtils.closeDialog();
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                }
            });


        }
    }

}
